package com.zjs.controller;

import com.github.pagehelper.PageInfo;
import com.github.stuxuhai.jpinyin.PinyinException;
import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.zjs.common.bean.PageQuery;
import com.zjs.common.bean.ResponseResult;
import com.zjs.pojo.BcRegion;
import com.zjs.service.BcRegionServiceI;
import com.zjs.service.Impl.BcRegionServiceImpl;
import com.zjs.vo.BcRegionVo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartResolver;

import javax.swing.plaf.synth.Region;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名： BcRegion
 * @说明： TODO
 * @作者：SS 苏帅出品
 * @时间：2019/12/5 16:29
 * @版本：1.0
 */
@RequestMapping(value = "bcRegion")
@Controller
public class BcRegionController {



    @Autowired
    BcRegionServiceImpl bcRegionService;


    //分页查看全部的  /bcRegion/findPage.action

    @RequestMapping(value = "findPage.action")
    @ResponseBody
    public Map<String,Object> findAllRegion(PageQuery query){


        PageInfo<BcRegion> regionByLimit = bcRegionService.findRegionByLimit(query);



        Map<String,Object> map=new HashMap<>();


        map.put("total",regionByLimit.getTotal());  //得到数据总数
        map.put("rows",regionByLimit.getList()); //得到List<BcRegion> 对象

        return  map;

    }



    //上传文件的解析
    @RequestMapping(value = "import.action")
    public String  importExcel(MultipartFile excelFile){

        try {
            //通过inputStream流得到一个workBook
            HSSFWorkbook workbook=new HSSFWorkbook(excelFile.getInputStream());

            //得到一个工作区
            HSSFSheet sheetAt = workbook.getSheetAt(0);

            //通过工作区得到一共多少行
            int lastRowNum = sheetAt.getLastRowNum();


            //创建一个对象用来存储BcRegion对象
            List<BcRegion> regions=new ArrayList<>();

            //for循环  i=1 表示从第二行开始循环 因为第一行为属性名
            for (int i=1;i<=lastRowNum;i++){

                 //得到每行的对象
                HSSFRow row = sheetAt.getRow(i);

                //通过每行对象得到 一共多少列
                int lastCellNum = row.getLastCellNum();

                //创建BcRegion对象
                BcRegion region=new BcRegion();


                for (int j=0;j<lastCellNum;j++){

                    //得到 每列的对象
                    HSSFCell cell = row.getCell(j);


                    switch (j){

                        //省份
                        case 1:
                            region.setProvince(cell.getStringCellValue());
                            break;
                            //城市
                        case 2:
                            region.setCity(cell.getStringCellValue());
                            break;
                            //区域
                        case 3:
                            region.setDistrict(cell.getStringCellValue());
                            break;
                               //邮编
                        case 4:
                            region.setPostcode(cell.getStringCellValue());
                            break;

                    }

                }


                //设置 每个对象的 shortCode和cityCode

                //1.对 city(城市)  省份 地区  进行去除最后一个字符
                String city = region.getCity();


                String province = region.getProvince();


                String district = region.getDistrict();

                String ProSub = province.substring(0, province.length() - 1);


                String citySub = city.substring(0, city.length() - 1);


                String DisSub = district.substring(0, district.length() - 1);


                //得到 城市简码  cityCode
                String CityPinyin = PinyinHelper.convertToPinyinString(citySub,"", PinyinFormat.WITHOUT_TONE);


                region.setCitycode(CityPinyin);


                //得到全称简码 shortCode
                String shortPinyin = PinyinHelper.getShortPinyin(ProSub+citySub+DisSub);


                region.setShortcode(shortPinyin);

                //将BcRegion对象存储到List集合中
                regions.add(region);

            }


            //添加

            int j=0;

            for (int i=0;i<regions.size();i++){
                int i1 = bcRegionService.saveBcRegionOrUp(regions.get(i));
                j+=i1;
            }

            if (j>0){
                return "/base/region";
            }


        } catch (IOException e) {
            e.printStackTrace();
        } catch (PinyinException e) {
            e.printStackTrace();
        }

        return  "/base/region";
    }




    //删除记录

    @RequestMapping(value = "delBcRegion.action")
    @ResponseBody
    public ResponseResult delBcRegionById(String ids){

        bcRegionService.delBcRegion(ids);

        return new ResponseResult(true,null);
    }


    //添加或更新
    @RequestMapping(value = "saveBcRegion.action")
    @ResponseBody  //返回的是一个json格式
    public ResponseResult  saveBcRegionOrUp(BcRegion region){

        int i = bcRegionService.saveBcRegionOrUp(region);

        if (i>0){
            return   new ResponseResult(true,null);
        }


        return    new ResponseResult(false,null);
    }

    //更新第一步 返回一个BcRegion对象

    @RequestMapping(value = "uPBcRegion.action")
    @ResponseBody
    public BcRegion uPBcRegionByIdOne(String id){

        BcRegion bcRegionById = bcRegionService.findBcRegionById(id);

        return bcRegionById;
    }


    //findAll.action  显示全部的 城市市区  在下拉框中

    @RequestMapping(value = "findAll.action")
    @ResponseBody
    public  List<BcRegionVo>  findRegionText(){

        List<BcRegionVo> bcRegionText = bcRegionService.findBcRegionText();

        return  bcRegionText;
    }


}
